home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / backup / flop-0.000 / flop-0 / flop-0.1 / unflop.c < prev   
C/C++ Source or Header  |  1994-06-06  |  1KB  |  75 lines

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4.  
  5. char buf [1024];
  6.  
  7. main (int argc, char **argv) {
  8.   int ifd;
  9.   unsigned long GrandTotalRead = 0;
  10.  
  11.   if ((ifd = open (argv [1], O_RDONLY)) < 0) {
  12.     fprintf (stderr, "open %s failed\n", argv [1]);
  13.     exit (1);
  14.   }
  15.  
  16.   while (1) {
  17.     char *p = buf;
  18.     int left = sizeof (buf);
  19.     int sofar = 0;
  20.     int eof = 0;
  21.  
  22.     while (left) {
  23.       int n;
  24.       n = read (ifd, p, left);
  25.       sofar += n;
  26.       GrandTotalRead += n;
  27.       left -= n;
  28.       p += n;
  29. /*      if (n == 0) {
  30.     eof++;
  31.     break;
  32.       }
  33.       else */ if (n < 0) {
  34.     fprintf (stderr, "read failed\n");
  35.     exit (1);
  36.       }
  37.       if (left != 0) {
  38.     FILE *tty;
  39.     close (ifd);
  40.     fprintf (stderr, "short read\n");
  41.     fprintf (stderr, "Total read: %ld\n", GrandTotalRead);
  42.     tty = fopen ("/dev/tty", "r");
  43.     if (!tty) {
  44.       fprintf (stderr, "open /dev/tty failed!\n");
  45.       exit (1);
  46.     }
  47.     fprintf (stderr, "hit any char when ready\n");
  48.     getc (tty);
  49.     fclose (tty);
  50.     if ((ifd = open (argv [1], O_RDONLY)) < 0 ) {
  51.       fprintf (stderr, "open %s failed\n", argv [1]);
  52.       exit (1);
  53.     }
  54.       }
  55.     }
  56.  
  57.     p = buf;
  58.     left = sofar;
  59.     sofar = 0;
  60.  
  61.     while (left) {
  62.       int n;
  63.       n = write (1, p, left);
  64.       if (n < 0) {
  65.     fprintf (stderr, "write error\n" );
  66.     exit (1);
  67.       }
  68.       sofar += n;
  69.       left -= n;
  70.       p += n;
  71.     }
  72.     if (eof) break;
  73.   }
  74. }
  75.